package com.kotlin.book.fragments

import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.kotlin.book.R
import com.kotlin.book.adapter.HomeFragmentAdapter
import com.kotlin.book.api.TypeApi
import com.kotlin.book.databinding.FragmentHomeBinding
import com.kotlin.book.util.Util

class HomeFragment : Fragment() {
    private lateinit var binding: FragmentHomeBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //获取首页navbar
        TypeApi.getHomeType(null) { response ->
            response?.data?.let {
                activity?.runOnUiThread {
                    //设置文字样式
                    for (type in it) {
                        val textView = TextView(context).apply {
                            layoutParams = LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                Util.dpToPx(30, requireContext())
                            )
                            setPadding(
                                Util.dpToPx(10, context),
                                Util.dpToPx(0, context),
                                Util.dpToPx(10, context),
                                Util.dpToPx(0, context)
                            )
                            textSize = 16f
                            text = type.name

                            setOnClickListener {
                                val index = binding.navbarBox.indexOfChild(it)
                                binding.homeContentViewPager.currentItem = index
                            }
                        }
                        binding.navbarBox.addView(textView)
                    }
                    //设置第一个选中
                    setTextViewStyle(0)
                    //设置页面绑定
                    binding.homeContentViewPager.adapter = HomeFragmentAdapter(this, it)
                    //监听翻页
                    binding.homeContentViewPager.registerOnPageChangeCallback(object :
                        ViewPager2.OnPageChangeCallback() {
                        override fun onPageSelected(position: Int) {
                            super.onPageSelected(position)
                            setTextViewStyle(position)
                        }
                    })
                }
            }
        }
    }

    private fun setTextViewStyle(position: Int) {
        for (i in 0 until binding.navbarBox.childCount) {
            val textView = binding.navbarBox.getChildAt(i) as TextView
            if (i == position) {
                textView.background =
                    ContextCompat.getDrawable(requireContext(), R.drawable.home_navbar_bottom)
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
                textView.setTypeface(null, Typeface.BOLD)
            } else {
                textView.background = null
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
                textView.setTypeface(null, Typeface.NORMAL)
            }
        }
    }
}